//
//  ViewController.m
//  autoLayoutXcode
//
//  Created by 曾泽昱 on 2024/7/11.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    UITextView* textView = [[UITextView alloc] init];
//    textView.backgroundColor = [UIColor redColor];
//    self.view.backgroundColor =[UIColor whiteColor];
//    
//    // 2. 启用Auto Layout
//    [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
//    [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
//
//    [NSLayoutConstraint constraintWithItem:textView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeading multiplier:1.0 constant:50];
//    
//    [NSLayoutConstraint constraintWithItem:textView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:121.0];
//    
//    [self.view addSubview:textView];
    
    // 假设你有一个UITextView实例和父视图SuperView实例
    UITextView *textView = [[UITextView alloc] init];
    UIView *superView = [[UIView alloc] init]; // 你的父视图实例

    // 1. 将textView添加到父视图中
    [superView addSubview:textView];

    // 2. 启用Auto Layout
    [superView setTranslatesAutoresizingMaskIntoConstraints:NO];
    [textView setTranslatesAutoresizingMaskIntoConstraints:NO];

    // 创建字典，用于约束中的视图引用
    NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(textView);

    // 创建居中约束
    NSArray *centerXConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(>=0)-[textView]-(>=0)-|" options:0 metrics:nil views:viewsDictionary];
    NSArray *centerYConstraints = [NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(>=0)-[textView]-(>=0)-|" options:0 metrics:nil views:viewsDictionary];

    // 将约束添加到superView
    [superView addConstraints:centerXConstraints];
    [superView addConstraints:centerYConstraints];
    
    // 可选：设置textView的其他属性
    textView.font = [UIFont systemFontOfSize:16];
    textView.textColor = [UIColor blackColor];
    self.view.backgroundColor = [UIColor whiteColor];
    NSLayoutConstraint* Ns；
    
}


@end
